package com.siwei.mes.upload.devops;

import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.upload.devops.config.DevopsConfig;
import com.siwei.mes.upload.devops.entity.DevopsMesUser;
import com.siwei.mes.upload.devops.entity.DevopsResultBody;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 上传到运维系统
 *
 * @Description: 上传到运维系统
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/1/14 11:25
 */
@Service
public class UploadDevopsService {
    private static final Logger log = LoggerFactory.getLogger("devops");

    @Resource
    private DevopsConfig devopsConfig;
    @Resource
    private RestTemplate restTemplate;

    /**
     * 获取访问地址
     *
     * @param path 接口名称
     * @return
     */
    public String getUrlPath(String path) {
        return devopsConfig.getAddress() + path;
    }

    /**
     * 上传品控用户
     *
     * @param userPhone 用户手机号
     */
    public void saveOrUpdateMesUser(String userPhone) {
        DevopsMesUser devopsMesUser = new DevopsMesUser();
        devopsMesUser.setUserPhone(userPhone);
        devopsMesUser.setCustomerId(devopsConfig.getCustomerId());
        HttpEntity httpEntity = new HttpEntity<>(devopsMesUser);
        this.uploadDevops(httpEntity, DevopsConfigEnum.DEVOPS_MES_SAVE_OR_UPDATE, false);
    }

    /**
     * 删除品控用户
     *
     * @param userPhone
     */
    public void deleteMesUser(String userPhone) {
        DevopsMesUser devopsMesUser = new DevopsMesUser();
        devopsMesUser.setUserPhone(userPhone);
        devopsMesUser.setCustomerId(devopsConfig.getCustomerId());
        HttpEntity httpEntity = new HttpEntity<>(devopsMesUser);
        this.uploadDevops(httpEntity, DevopsConfigEnum.DEVOPS_MES_DELETE, false);
    }


    /**
     * @param httpEntity       发送的参数
     * @param devopsConfigEnum 接口枚举
     * @param isNotify         是否报错通知 true 通知 false不通知
     */
    private void uploadDevops(HttpEntity httpEntity, DevopsConfigEnum devopsConfigEnum, Boolean isNotify) {
        if (StringUtils.isBlank(devopsConfig.getAddress()) || StringUtils.isBlank(devopsConfig.getCustomerId())) {
            log.info("砼学运维系统配置为空，不上传！");
            return;
        }
        String url = this.getUrlPath(devopsConfigEnum.getPath());

        log.info("上传到devops【" + devopsConfigEnum.getDesc() + "】,接口地址：【" + url + "】,入参：" + JsonUtils.getJson(httpEntity.getBody()));
        /**发起请求,服务地址，请求参数，返回消息体的数据类型*/
        try {
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, httpEntity, JSONObject.class);
            JSONObject body = response.getBody();
            DevopsResultBody devopsResultBody = JsonUtils.getObject(body, DevopsResultBody.class);
            if (devopsResultBody == null) {
                assertTrue("devops服务异常,请联系devops相关人员排查");
            }
            if (devopsResultBody.getCode() == 1) {
                log.info("上传到devops【{}】成功,返回信息：【{}】", devopsConfigEnum.getDesc(), response.getBody());
            } else {
                log.info("上传到devops【{}】失败,错误信息：【{}】，入参：【{}】", devopsConfigEnum.getDesc(), devopsResultBody.getMsg(), JsonUtils.getJson(httpEntity.getBody()));
                assertTrue(isNotify, "上传到devops【" + devopsConfigEnum.getDesc() + "】失败,错误信息：【" + devopsResultBody.getMsg() + "】");
            }
        } catch (Exception e) {
            log.error("上传到devops【{}】异常,错误信息：【{}】入参：【{}】", devopsConfigEnum.getDesc(), e.getMessage(), JsonUtils.getJson(httpEntity.getBody()), e);
            assertTrue(isNotify, "上传到devops【" + devopsConfigEnum.getDesc() + "】异常，错误信息：【" + e.getMessage() + "】");
        }
    }
}
